﻿using System.Collections.Generic;
using UnityEngine;

public class UIListView : UIComponent {
	// === Public =====================================================================================================
	public UIListView(Vector2 position, int numberColumns = 1) {
		_numberColumns = numberColumns <= 0 ? 1 : numberColumns;
		Init(position);
	}

	public List<UIListItem> GetItems() {
		return _items;
	}

	public void Add(params UIListItem[] listItem) {
		foreach (var uiListItem in listItem) {
			var posX = GetLocation().x + OFFSET + _items.Count % _numberColumns * OFFSET + _items.Count % _numberColumns * BUTTON_WIDTH;
			var posY = GetLocation().y + OFFSET + (_items.Count / _numberColumns) * OFFSET + (_items.Count / _numberColumns) * BUTTON_HEIGHT;
			uiListItem.SetLocation((int)posX, (int)posY);
			uiListItem.GetUITexture().SetLocation((int)posX + OFFSET, (int)posY + OFFSET);
			uiListItem.GetUILabel().SetLocation((int)posX, (int)posY + OFFSET + (int)uiListItem.GetUITexture().GetSize().y);
			_items.Add(uiListItem);
		}
	}

	// === Private ====================================================================================================
	private readonly List<UIListItem> _items = new List<UIListItem>();
	private readonly int _numberColumns;
	private const int OFFSET = 10;
	private const int BUTTON_WIDTH = 80;
	private const int BUTTON_HEIGHT = 100;

	private void Init(Vector2 position) {
		SetName(GetType().ToString());
		SetLocation(position);
		var sizeX = OFFSET * 2 + BUTTON_WIDTH * _numberColumns + OFFSET * (_numberColumns - 1);
		SetSize(sizeX, Screen.height - (int)position.y);
		UpdateStyle();
	}
}
